package com.wjerp.tplus.server.common.security;

import com.wjerp.tplus.server.domain.entity.sys.UserInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author lishuailei
 */
public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    public static JwtUser create(UserInfo user, List<String> roles,String companyCode) {
    	boolean isEnabled = false;
    	if(user.getEnabled()){
    		isEnabled = true;
    	}else{
    		isEnabled = false;
    	}
    	
        JwtUser tmpU =  new JwtUser(
                user.getId(),
                user.getDisplayUserId(),
                user.getMobile(),
                user.getPassword(),
                user.getUserType(),
                mapToGrantedAuthorities(roles),
                isEnabled,
                companyCode);
        
        return tmpU;
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
    	
        return authorities.stream()
                .map(authority -> new SimpleGrantedAuthority(authority))
                .collect(Collectors.toList());
        
    }
}
